package com.gc.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gc.business.domain.GovRegion;
import com.gc.business.mapper.GovRegionMapper;
import com.gc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gc.business.mapper.AdminRegionMapper;
import com.gc.business.domain.AdminRegion;
import com.gc.business.service.IAdminRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

/**
 * 行政区域管理 服务层实现
 * 
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Service
public class AdminRegionServiceImpl extends ServiceImpl<AdminRegionMapper, AdminRegion> implements IAdminRegionService {

    @Autowired
    private GovRegionMapper govRegionMapper;

    @Override
    public List<AdminRegion> list(AdminRegion adminRegion){
        LambdaQueryWrapper<AdminRegion> wrapper = new LambdaQueryWrapper<>();
        if(adminRegion != null){
            if(StringUtils.isNotEmpty(adminRegion.getRegionId())){
                wrapper.eq(AdminRegion::getRegionId, adminRegion.getRegionId());
            }
            if(StringUtils.isNotEmpty(adminRegion.getRegionTitle())){
                wrapper.like(AdminRegion::getRegionTitle, adminRegion.getRegionTitle());
            }
            if(StringUtils.isNotEmpty(adminRegion.getCreateBy())){
                wrapper.eq(AdminRegion::getCreateBy, adminRegion.getCreateBy());
            }
            if(StringUtils.isNotEmpty(adminRegion.getCreateTime())){
                wrapper.eq(AdminRegion::getCreateTime, adminRegion.getCreateTime());
            }
            if(StringUtils.isNotEmpty(adminRegion.getUpdateBy())){
                wrapper.eq(AdminRegion::getUpdateBy, adminRegion.getUpdateBy());
            }
            if(StringUtils.isNotEmpty(adminRegion.getUpdateTime())){
                wrapper.eq(AdminRegion::getUpdateTime, adminRegion.getUpdateTime());
            }
            if(StringUtils.isNotEmpty(adminRegion.getRemark())){
                wrapper.eq(AdminRegion::getRemark, adminRegion.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<AdminRegion> selectAdminRegionList(AdminRegion adminRegion){
        return baseMapper.selectAdminRegionList(adminRegion);
    }

    @Override
    public List<AdminRegion> selectAdminRegion(){
        return baseMapper.selectAdminRegion();
    }

    /**
     * 转树结构
     */
    @Override
    public List<Map<String, Object>> selectDeptTree(Long attrId){
        List<Map<String, Object>> trees;
        //全部查询
        if(StringUtils.isNull(attrId) || attrId == 0){
            List<AdminRegion> regionList = baseMapper.selectAdminRegion();
            trees = getTrees(regionList);
        }else{
            //根据管理辖区查询
            List<GovRegion> govRegionList = govRegionMapper.selectGovRegionByGovId(attrId);
            List<AdminRegion> regionList = new ArrayList<>();
            for (GovRegion govRegion : govRegionList) {
                if(govRegion == null){
                    continue;
                }
                AdminRegion adminRegion = baseMapper.selectById(govRegion.getRegionId());
                regionList.add(adminRegion);
            }
            trees = getTrees(regionList);
        }
        return trees;
    }


    /**
     * 对象转部门树
     *
     * @param regionList  区域列表
     * @return
     */
    private List<Map<String, Object>> getTrees(List<AdminRegion> regionList) {
        List<Map<String, Object>> trees = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>(5);
        map1.put("name","全部");
        map1.put("id","");
        trees.add(map1);
        for (AdminRegion region : regionList) {
            Map<String, Object> regionMap = new HashMap<>(5);
            regionMap.put("id", region.getRegionId());
            regionMap.put("name", region.getRegionTitle());
            regionMap.put("title", region.getRegionTitle());
            trees.add(regionMap);
        }
        return trees;
    }

}
